package com.wxapp.admin.service.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wxapp.admin.api.pojo.entity.CompanyDeptDO;
import com.wxapp.admin.api.pojo.entity.DeptJobDO;
import com.wxapp.admin.api.pojo.entity.JobDO;
import com.wxapp.admin.api.pojo.vm.job.PJobDetailVm;
import com.wxapp.admin.api.pojo.vm.job.PJobPageVm;
import com.wxapp.admin.api.pojo.vo.job.QJobPageVo;
import com.wxapp.admin.api.pojo.vo.job.QJobSaveVo;
import com.wxapp.admin.api.pojo.vo.job.QJobUpdateVo;
import com.wxapp.admin.service.mapper.CompanyDeptMapper;
import com.wxapp.admin.service.mapper.DeptJobMapper;
import com.wxapp.admin.service.mapper.JobMapper;
import com.wxapp.admin.service.service.JobService;
import com.wxapp.common.constant.base.IBaseService;
import com.wxapp.common.constant.pojo.vm.ResultVm;
import com.wxapp.common.constant.pojo.vo.UserVo;
import com.wxapp.common.util.check.CheckUtil;
import com.wxapp.common.util.check.CompareUtil;
import com.wxapp.common.util.condition.ConditionUtil;
import com.wxapp.common.util.copy.CopyUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * @author lgl
 * @Description
 * @Datetime 2022/8/8 17:20
 * @Modified By
 */
@Service
@RequiredArgsConstructor
public class JobServiceImpl extends IBaseService implements JobService {
    private final JobMapper jobMapper;
    private final DeptJobMapper deptJobMapper;
    private final CompanyDeptMapper companyDeptMapper;

    @Override
    public ResultVm<String> saveJob(UserVo userVo, QJobSaveVo qJobSaveVo) {
        LambdaQueryWrapper<CompanyDeptDO> cdWrapper = new LambdaQueryWrapper<>();
        cdWrapper.eq(CompanyDeptDO::getCompanyId, qJobSaveVo.getCompanyId()).eq(CompanyDeptDO::getDeptId, qJobSaveVo.getDeptId()).eq(CompanyDeptDO::getStatus, true);
        var aLong = companyDeptMapper.selectCount(cdWrapper);
        if (!CompareUtil.oneBigLong(aLong, 0L)) {
            return error("该公司下没有该部门");
        }
        LambdaQueryWrapper<JobDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(JobDO::getName, qJobSaveVo.getName()).eq(JobDO::getStatus, true);
        var count = jobMapper.selectCount(wrapper);
        if (count > 0) {
            return error("已经定义该名称岗位");
        }
        var jobDO = CopyUtil.copyObjectNew(qJobSaveVo, JobDO::new, (s, r) -> {
            r.setCreateBy(userVo.getId());
            return null;
        });
        jobMapper.insert(jobDO);
        DeptJobDO deptJobDO = new DeptJobDO();
        deptJobDO.setDeptId(qJobSaveVo.getDeptId());
        deptJobDO.setJobId(jobDO.getId());
        deptJobDO.setCreateBy(userVo.getId());
        deptJobMapper.insert(deptJobDO);
        return success("保存岗位成功");
    }

    @Override
    public ResultVm<String> updateJob(UserVo userVo, QJobUpdateVo qSaveVo) {
        LambdaQueryWrapper<CompanyDeptDO> cdWrapper = new LambdaQueryWrapper<>();
        cdWrapper.eq(CompanyDeptDO::getCompanyId, qSaveVo.getCompanyId()).eq(CompanyDeptDO::getDeptId, qSaveVo.getDeptId()).eq(CompanyDeptDO::getStatus, true);
        var aLong = companyDeptMapper.selectCount(cdWrapper);
        if (!CompareUtil.oneBigLong(aLong, 0L)) {
            return error("该公司下没有该部门");
        }
        var jobDO = jobMapper.selectById(qSaveVo.getId());
        if (CheckUtil.isNullObject(jobDO)) {
            return error("没有找到岗位信息");
        }
        CopyUtil.copyObject(qSaveVo, jobDO, (s, r) -> {
            r.setUpdateBy(userVo.getId());
            return null;
        });
        jobMapper.updateById(jobDO);
        if (CheckUtil.isNotNullObject(qSaveVo.getDeptId())) {
            LambdaUpdateWrapper<DeptJobDO> wrapper = new LambdaUpdateWrapper<>();
            wrapper.eq(DeptJobDO::getJobId, qSaveVo.getId()).eq(DeptJobDO::getDeptId, qSaveVo.getDeptId()).eq(DeptJobDO::getStatus, true).set(DeptJobDO::getStatus, false);
            deptJobMapper.update(null, wrapper);
            DeptJobDO deptJobDO = new DeptJobDO();
            deptJobDO.setDeptId(qSaveVo.getDeptId());
            deptJobDO.setJobId(jobDO.getId());
            deptJobDO.setCreateBy(userVo.getId());
            deptJobMapper.insert(deptJobDO);
        }
        return success("更新岗位成功");
    }

    @Override
    public ResultVm<PJobDetailVm> detailJob(String id) {
        var jobDO = jobMapper.selectById(id);
        if (CheckUtil.isNullObject(jobDO)) {
            return error("没有找到岗位信息");
        }
        LambdaQueryWrapper<DeptJobDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DeptJobDO::getJobId, id).eq(DeptJobDO::getStatus, true);
        var deptJobDO = deptJobMapper.selectFirst(wrapper);
        LambdaQueryWrapper<CompanyDeptDO> cdWrapper = new LambdaQueryWrapper<>();
        cdWrapper.eq(CompanyDeptDO::getDeptId, deptJobDO.getDeptId()).eq(CompanyDeptDO::getStatus, true);
        var companyDeptDO = companyDeptMapper.selectFirst(cdWrapper);
        var pDetailVm = CopyUtil.copyObjectNew(jobDO, PJobDetailVm::new, null);
        pDetailVm.setDeptId(deptJobDO.getDeptId());
        pDetailVm.setCompanyId(companyDeptDO.getCompanyId());
        return success(pDetailVm);
    }

    @Override
    public ResultVm<Page<PJobPageVm>> pageJob(QJobPageVo qSaveVo) {
        var page = ConditionUtil.getPage(qSaveVo, JobDO.class);
        Page<PJobPageVm> pages = jobMapper.selectJob(page, qSaveVo);
        return success(pages);
    }

    @Override
    public ResultVm<String> deleteJob(String id) {
        var jobDO = jobMapper.selectById(id);
        if (CheckUtil.isNullObject(jobDO)) {
            return error("没有找到岗位信息");
        }
        jobDO.setStatus(false);
        jobMapper.updateById(jobDO);
        LambdaUpdateWrapper<DeptJobDO> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(DeptJobDO::getJobId, id).eq(DeptJobDO::getStatus, true).set(DeptJobDO::getStatus, false);
        deptJobMapper.update(null, wrapper);
        return success("删除岗位成功");
    }
}
